<?php

namespace Acme\SecurityBundle\Controller;

use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

use Acme\UserBundle\Entity\CmUser;
use Acme\UserBundle\Entity\CmUserInfo;
use Acme\UserBundle\Entity;
use Acme\CommonBundle\Entity\ConstUniversity;

use Acme\UserBundle\Form\UserInfoType;


class EmailValidatorController extends Controller
{
    /**
     * @Route("/email_valid/{id}/{salt}", defaults={"id" = "", "salt" = ""}, name="AcmeSecurityBundle_EmailValid")
     * @Template()
     */
    public function emailValidAction($id, $salt)
    {
    	$em = $this->getDoctrine()->getEntityManager();
    	$user = $em->getRepository('AcmeUserBundle:CmUser')->find($id);
    	
    	if (!$user) {
    		throw $this->createNotFoundException('No user found for id ' . $id);
    	}
    	
    	if ($user->getEmailStatus() == array_search('not-active', $this->container->getParameter('emailStatusEn')) && $salt == $user->getSalt()) {
    		$user->setEmailStatus(array_search('is-active', $this->container->getParameter('emailStatusEn')));
    		$em->flush();
    		return array('user' => $user);
    	}
    	return new RedirectResponse($this->generateUrl('AcmeSecurityBundle_Login'));
    }
}
